package cn.itsource.ymcc.service.impl;

import cn.itsource.ymcc.client.LogininfoClient;
import cn.itsource.ymcc.domain.*;
import cn.itsource.ymcc.dto.RegDto;
import cn.itsource.ymcc.mapper.UserBaseInfoMapper;
import cn.itsource.ymcc.mapper.UserGrowLogMapper;
import cn.itsource.ymcc.mapper.UserMapper;
import cn.itsource.ymcc.mapper.UserRealInfoMapper;
import cn.itsource.ymcc.result.JsonResult;
import cn.itsource.ymcc.service.IUserService;
import cn.itsource.ymcc.util.AssertUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author whale.chen
 * @since 2023-02-26
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private UserBaseInfoMapper userBaseInfoMapper;
    @Autowired
    private UserRealInfoMapper userRealInfoMapper;
    @Autowired
    private UserGrowLogMapper userGrowLogMapper;
    @Autowired
    private LogininfoClient logininfoClient;
    /**
     * registe用户提交注册
     */
    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public void register(RegDto regDto) {

        //1.校验验证码是否过期
        AssertUtil.isTrue(redisTemplate.hasKey(regDto.getMobile()),"验证码已过期");
        //2.redis校验验证码是否相同
        String coud = (String)redisTemplate.opsForValue().get(regDto.getMobile());
        AssertUtil.isTrue(coud.equals(regDto.getSmsCode()),"手机验证码错误");
        //3.验证手机号是否被注册
        User phone = baseMapper.selectOne(new QueryWrapper<User>()
                .eq("phone", regDto.getMobile()));
        AssertUtil.isTrue(phone==null,"该手机号已经被注册!");

        //openFeign调用添加Logininfo表
        Logininfo logininfo = new Logininfo()
                .setUsername(regDto.getMobile())
                .setPassword(new BCryptPasswordEncoder().encode(regDto.getPassword()));
        JsonResult jsonResult = logininfoClient.saveOrUpdate(logininfo);
        Long logininfoId = ((Integer) jsonResult.getData()).longValue();

        //4.保存user表数据 MyBatisPlus默认返回主键
        User user = new User()
                .setPhone(regDto.getMobile())
                .setNickName(regDto.getMobile())
                .setLoginId(logininfoId);
        baseMapper.insert(user);

        //5.保存UserBaseInfo表
        UserBaseInfo userBaseInfo = new UserBaseInfo()
                .setUserId(user.getId())
                .setRegChannel(regDto.getRegChannel());
        userBaseInfoMapper.insert(userBaseInfo);

        //6.保存UserRealInfo表
        UserRealInfo userRealInfo = new UserRealInfo()
                .setUserId(user.getId());
        userRealInfoMapper.insert(userRealInfo);

        //7.保存UserGrowLog表
        UserGrowLog userGrowLog = new UserGrowLog()
                .setUserId(user.getId())
                .setFromReason("新用户注册")
                .setScore(50);
        userGrowLogMapper.insert(userGrowLog);
    }
}
